package com.changanw.tienchin.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.changanw.tienchin.activity.domain.Activity;
import com.changanw.tienchin.activity.domain.vo.ActivityVo;
import com.changanw.tienchin.activity.mapper.ActivityMapper;
import com.changanw.tienchin.activity.service.IActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.core.domain.model.PieData;
import com.changanw.tienchin.common.utils.SecurityUtils;
import com.changanw.tienchin.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author changanw
 * @since 2023-11-21
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {
  @Autowired
  ActivityMapper activityMapper;
  @Override
  public List<ActivityVo> selectChannelList(ActivityVo activityVo) {
    expireActivity();
    return activityMapper.selectChannelList(activityVo);
  }

  @Override
  public AjaxResult addActivity(ActivityVo activityVo) {
    /*QueryWrapper<Activity> qw = new QueryWrapper<>();
    //查询当前名称并且是没有删除的
    qw.lambda().eq(Activity::getActivityName,activityVo.getActivityName()).eq(Activity::getDelFlag,0);
    Activity c = getOne(qw);
    if (c != null){
      //说明存在同名的渠道，并且没有删除
      return AjaxResult.error("存在同名渠道，添加失败");
    }*/
    Activity activity = new Activity();
    BeanUtils.copyProperties(activityVo,activity);
    activity.setCreateBy(SecurityUtils.getUsername());
    activity.setCreateTime(LocalDateTime.now());
    activity.setDelFlag(0);
    //新增的默认未过期
    activity.setStatus(1);
    //activity.setChannelId(activityVo.getChannelId());

    return save(activity) ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
  }

  @Override
  public AjaxResult updateChannel(ActivityVo activityVo) {
    Activity activity = new Activity();
    BeanUtils.copyProperties(activityVo,activity);
    activity.setUpdateBy(SecurityUtils.getUsername());
    activity.setUpdateTime(LocalDateTime.now());
    activity.setStatus(1);
    //防止这三个属性被修改
    activity.setCreateTime(null);
    activity.setCreateBy(null);
    activity.setDelFlag(null);
    return updateById(activity) ? AjaxResult.success("更新成功"):AjaxResult.error("更新失败");
  }

  @Override
  public ActivityVo getActivityById(Long activityId) {
    Activity activity = getById(activityId);
    ActivityVo activityVo = new ActivityVo();
    BeanUtils.copyProperties(activity,activityVo);
    return activityVo;
  }

  @Override
  public boolean deleteChannelByIds(Long[] activityIds) {
    UpdateWrapper<Activity> uw = new UpdateWrapper<>();
    uw.lambda().set(Activity::getDelFlag,1).in(Activity::getActivityId,activityIds);
    return update(uw);
  }

  @Override
  public AjaxResult selectActivityByChannelId(Integer channelId) {
    QueryWrapper<Activity> qw = new QueryWrapper<>();
    qw.lambda().eq(Activity::getChannelId, channelId);

    return AjaxResult.success(list(qw));
  }

  /**
   *  将活动结束日期小于当前日期的 状态改完 过期（0）
   * @return
   */
  public boolean expireActivity(){
    UpdateWrapper<Activity> uw = new UpdateWrapper();
    //此处表示 将结束时间小于当前时间的活动 并且活动状态是1的 更新为0
    uw.lambda().set(Activity::getStatus,0).eq(Activity::getStatus,1).lt(Activity::getEndTime, LocalDateTime.now());
    return update(uw);
  }

  @Override
  public AjaxResult activityAnalysis(ActivityVo activityVo) {
    //设置一个默认时间区间，默认查询前后一周的数据
    if (activityVo.getParams().get("beginTime") == null ||
            activityVo.getParams().get("endTime") == null){
      activityVo.getParams().put("beginTime",LocalDateTime.now().minusWeeks(1));
      activityVo.getParams().put("endTime",LocalDateTime.now().plusWeeks(1));
    }
    List<PieData> pieData = activityMapper.activityAnalysis(activityVo);

    return AjaxResult.success(pieData);
  }
}
